package com.wench.design.pattern.singleton.lazy.demo5;

import java.io.*;

/**
 * 破坏方式：序列化方式破坏单例模式
 * 演示路径：/Users/wench/Develop/demo
 *
 * @author wench
 */
public class SerializableClient {

    public static void main(String[] args) throws Exception {
//        writeObject2File();
        Singleton singleton = readObjectFromFile();
        Singleton instance = readObjectFromFile();
        System.out.println(singleton == instance);
    }

    public static Singleton readObjectFromFile() throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/Users/wench/Develop/demo/a.txt"));
        Singleton instance = (Singleton) ois.readObject();
        return instance;
    }

    public static void writeObject2File() throws IOException {
        Singleton instance = Singleton.getInstance();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/wench/Develop/demo/a.txt"));
        oos.writeObject(instance);
        oos.close();
    }
}
